var mongoose = require('mongoose')
var db = require('../config/db')
var Schema = mongoose.Schema;

// 用户表
var UserSchema = new Schema({
    name:{type:String},
    pwd:{type:String},
    email:{type:String},
    sex:{type:String,default:'asexual'},
    birth:{type:Date},
    phone:{type:String},
    explain:{type:String},
    imgurl:{type:String},
    time:{type:Date},
})

// 好友表
var FriendSchema = new Schema({
    userID:{type:Schema.Types.ObjectId,ref:'User'},
    friendID:{type:Schema.Types.ObjectId,ref:'User'},
    state:{type:String},        // 好友状态 0 已为好友、1 申请中、2 申请发送中
    time:{type:Date},
    lasttime:{type:Date},       // 最后的消息时间
})

// 一对一消息表
var MessageSchema = new Schema({
    userID:{type:Schema.Types.ObjectId,ref:'User'},
    friendID:{type:Schema.Types.ObjectId,ref:'User'},
    message:{type:String},
    types:{type:String},        // 0 文字 1 图片连接 2 音频连接
    time:{type:Date},
    state:{type:Number},        // 0 已读 1 未读
})

// 群表
var GroupSchema = new Schema({
    userID:{type:Schema.Types.ObjectId,ref:'User'},
    name:{type:String},
    imgurl:{type:String},        
    time:{type:Date},
    lasttime:{type:Date},       // 最后的消息时间
    notice:{type:Number},        
})

// 群成员表
var GroupUserSchema = new Schema({
    groupID:{type:Schema.Types.ObjectId,ref:'Group'},
    userID:{type:Schema.Types.ObjectId,ref:'User'},
    name:{type:String},
    tip:{type:Number,default:0},        
    time:{type:Date},
    shield:{type:Number},        
})

// 群消息表
var GroupMsgSchema = new Schema({
    groupID:{type:Schema.Types.ObjectId,ref:'Group'},
    userID:{type:Schema.Types.ObjectId,ref:'User'},
    message:{type:String},   
    type:{type:String},   
    time:{type:Date},  
})


module.exports = db.model("User",UserSchema);
module.exports = db.model("Friend",FriendSchema);
module.exports = db.model("Message",MessageSchema);
module.exports = db.model("Group",GroupSchema);
module.exports = db.model("GroupUser",GroupUserSchema);
module.exports = db.model("GroupMsg",GroupMsgSchema);


